;; Copyright (C) 2011, 2012, 2013 Markus Kollmar
;;
;; This file is part of ESCAD.
;;
;; ESCAD is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; ESCAD is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with ESCAD.  If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(in-package "COMMON-LISP-USER")
(defpackage :de.markus-herbert-kollmar.escad.report
  (:use :common-lisp :escad)
  (:nicknames :escad.report)
  (:export :report2html :report2txt)
  (:shadow #:cos)
  (:documentation "Analyse schematic and extract basic information to text or html format."))

(in-package :de.markus-herbert-kollmar.escad.report)


;; USER CONFIG START
(defparameter *html-header* "<!-- ** Generated by escad expansion report. ** -->" "Will be inserted in html")
(defparameter *html-css* "" "This is a string which will be inserted inline in html document and you can define css.")
(defparameter *text-header* "** Generated by escad expansion report. **" "will be inserted at beginning of generated text document.")
;; USER CONFIG END

(defun report2html (expansion-symbol-name &optional (filename "report.html"))
  "expansion-symbol-name [relative-file-name] ->
Analyse view and try to report view to a html file with name <report.html>, which is viewable by a internet browser with svg support."
  (let* ((output-filename (concatenate 'string *escad-view-dir*
				      (call-expansion-function "escad.symbol._escad.export.svg"
							       "export2svg" expansion-symbol-name)))
	(title "ESCAD-HTML-REPORT: ")
	(css *html-css*)
	(description (with-output-to-string (stream)
			(dolist (name (ls :exclude-taxonomy '("escad.symbol._escad" "escad.symbol._thisView")))
			  (format stream "<p>") (format stream name) (format stream " (")
			  (format stream (sp name 'escad:taxonomy)) (format stream "):")
			  (let ((_comment (sp name 'escad:comment)))
			    (if _comment (format stream _comment) (format stream "")))
			  (format stream "</p>"))))
	(svg-schematic (file-data2string output-filename)))

    (with-open-file (out filename :direction :output :if-exists :supersede)
		    (with-standard-io-syntax
		     (princ *html-header* out) (write-char #\newline out)
		     (princ "
<!DOCTYPE html>  
<html>  
  <head>  
    <title>" out)
		     (princ title out)
		     (princ "
</title>" out)
		     (princ "<style type=\"text/css\">" out)
		     (princ css out)
		     (princ "</style>
    <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\" />
  </head>  
  <body>" out)
		     (princ svg-schematic out)
		     (princ description out)
		     (princ "
  </body>  
</html>" out)
		     (write-char #\newline out)))
    output-filename))

(defun report2txt (expansion-symbol-name &optional (filename "report.txt"))
  "expansion-symbol-name [relative-file-name] ->
Analyse view and try to report view to a textual file with name <report.txt>, which is viewable by a text editor."
  (let ((output-filename (concatenate 'string *escad-view-dir* filename)))
    (with-open-file (out output-filename
			 :direction :output
			 :if-exists :supersede)
		    (with-standard-io-syntax
		     (princ *text-header* out) (write-char #\newline out)
		     (princ "Symbols:" out) (write-char #\newline out)
		     (dolist (name (ls))
		       (princ (concatenate 'string name ";") out)
		       (write-char #\newline out))
		     (princ "Relations:" out) (write-char #\newline out)
		     (dolist (name (lr))
		       (with-slots (comment ref_from ref_to taxonomy) (r name)
				   (loop for i from 0 to (- (length ref_from) 1) do
					 (princ (concatenate 'string (string (nth i ref_from))
							     " -> "
							     (string (nth i ref_to))
							     ";") out)
					 (write-char #\newline out))))
		     (write-char #\newline out)
		     (princ "END Report" out)))
    output-filename))

